package com.spring;

import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class JedisHash {
    public static void test1(Jedis jedis) {
        System.out.println("hget 和 hset 的使用");
        jedis.flushAll();


        jedis.hset("key1","f1","111");
        jedis.hset("key1","f2","222");
        jedis.hset("key1","f3","333");

        String hget = jedis.hget("key1", "f2");
        System.out.println(hget);
    }

    public static void test2(Jedis jedis) {
        System.out.println("hexist 和 hdel 的使用");
        jedis.flushAll();


        jedis.hset("key1","f1","111");
        jedis.hset("key1","f2","222");
        jedis.hset("key1","f3","333");

        boolean hexists = jedis.hexists("key1", "f2");
        System.out.println(hexists);

        jedis.hdel("key1", "f2","f3");
        hexists=jedis.hexists("key1", "f3");
        System.out.println(hexists);

    }

    public static void test3(Jedis jedis) {
        System.out.println("hkeys 和 hvals 的使用");
        jedis.flushAll();


        jedis.hset("key1","f1","111");
        jedis.hset("key1","f2","222");
        jedis.hset("key1","f3","333");

        Set<String> key1 = jedis.hkeys("key1");
        System.out.println(key1);

        List<String> key11 = jedis.hvals("key1");
        System.out.println(key11);
    }

    public static void test4(Jedis jedis) {
        System.out.println("hmget 和 hmset 的使用");
        jedis.flushAll();

        Map<String,String> map = new HashMap<String,String>();
        map.put("f1","111");
        map.put("f2","222");
        map.put("f3","333");

        String key11 = jedis.hmset("key1", map);
        System.out.println(key11);

        List<String> key1 = jedis.hmget("key1","f1","f2","f100");
        System.out.println(key1);
    }

    public static void main(String[] args) {
        JedisPool jedispool=new JedisPool("tcp://127.0.0.1:8888");

        try(Jedis jedis=jedispool.getResource()){
            jedis.auth("123456");
//            test1(jedis);
//            test2(jedis);
//            test3(jedis);
            test4(jedis);
        }
    }
}
